using System;
using System.IO.IsolatedStorage;

namespace MM.Translator.Configuration
{
    public class AppSettings
    {
        private const string SourceLanguageKeyName = "SourceLanguageKey";
        private const string TargetLanguageKeyName = "TargteLanguageKey";

        private readonly IsolatedStorageSettings _isolatedStore;
        private static AppSettings _appSettings;

        public AppSettings()
        {
            _isolatedStore = IsolatedStorageSettings.ApplicationSettings;
        }

        /// <summary>
        /// Update a setting value for our application. If the setting does not
        /// exist, then add the setting.
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public bool AddOrUpdateValue(string key, Object value)
        {
            var valueChanged = false;

            // If the key exists
            if (_isolatedStore.Contains(key))
            {
                // If the value has changed
                if (_isolatedStore[key] != value)
                {
                    // Store the new value
                    _isolatedStore[key] = value;
                    valueChanged = true;
                }
            }
            // Otherwise create the key.
            else
            {
                _isolatedStore.Add(key, value);
                valueChanged = true;
            }

            return valueChanged;
        }

        /// <summary>
        /// Get the current value of the setting, or if it is not found, set the 
        /// setting to the default setting.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <param name="defaultValue"></param>
        /// <returns></returns>
        public T GetValueOrDefault<T>(string key, T defaultValue)
        {
            T value;

            // If the key exists, retrieve the value.
            if (_isolatedStore.Contains(key))
            {
                value = (T)_isolatedStore[key];
            }
            // Otherwise, use the default value.
            else
            {
                value = defaultValue;
            }

            return value;
        }

        /// <summary>
        /// Save the settings.
        /// </summary>
        public void Save()
        {
            _isolatedStore.Save();
        }

        public string SourceLanguage
        {
            get { return GetValueOrDefault(SourceLanguageKeyName, "en"); }
            set
            {
                AddOrUpdateValue(SourceLanguageKeyName, value);
                Save();
            }
        }

        public string TargetLanguage
        {
            get { return GetValueOrDefault(TargetLanguageKeyName, "fa"); }
            set
            {
                AddOrUpdateValue(TargetLanguageKeyName, value);
                Save();
            }
        }

        public static AppSettings Current
        {
            get { return _appSettings ?? (_appSettings = new AppSettings()); }
        }
    }
}